package kburndown.client.app.controls
{
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;

	import mx.core.UIComponent;

	public class TaskItemShadow extends UIComponent
	{
		// --------------------------------------------------
		//
		//		类定义
		//
		// --------------------------------------------------

		// --------------------------------------------------
		//		常量
		// --------------------------------------------------
		private const ALPHA_SPEED: Number = 0.15;

		// --------------------------------------------------
		//		类属性
		// --------------------------------------------------
		private var _glowEnabled: Boolean = false;

		public function get glowEnabled(): Boolean
		{
			return _glowEnabled;
		}

		public function set glowEnabled(value: Boolean): void
		{
			if (value != _glowEnabled)
			{
				_glowEnabled = value;
			}
		}

		// --------------------------------------------------
		//		构造函数
		// --------------------------------------------------
		public function TaskItemShadow()
		{
			super();

			shadowSprite = new Sprite();
			shadowSprite.alpha = 0;
			this.addChild(shadowSprite);
			g = shadowSprite.graphics;

			boundSprite = new Sprite();
			this.addChild(boundSprite);

			glowFilter = new GlowFilter(0x1CB007);
			glowFilter.strength = .9;
			glowFilter.blurX = 8;
			glowFilter.blurY = 8;
			glowFilter.alpha = 0;
			glowFilter.knockout = true;
			boundSprite.filters = [glowFilter];
		}

		// --------------------------------------------------
		//		私有成员
		// --------------------------------------------------
		private var shadowSprite: Sprite;
		private var boundSprite: Sprite;
		private var g: Graphics;
		private var glowFilter: GlowFilter;

		private function drawBound(): void
		{
			var g: Graphics = boundSprite.graphics;

			g.clear();
			g.beginFill(0xFF9900);
			g.drawRoundRect(0, 0, this.width, this.height, (_glowEnabled ? 12 : 0));
			g.endFill();
		}

		private function drawShadows(): void
		{
			g.clear();
			var mtx: Matrix = new Matrix();
			mtx.createGradientBox(this.width, this.height, Math.PI / 2);
			g.beginGradientFill(GradientType.LINEAR, [0x000000, 0x000000], [0, .05], [0x00, 0xFF], mtx);
			g.drawRoundRect(0, 0, this.width, this.height, (_glowEnabled ? 12 : 0));
			g.endFill();
		}

		override protected function updateDisplayList(unscaledWidth: Number, unscaledHeight: Number): void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);

			drawBound();
			drawShadows();
		}

		private var _alphaTo: Number;

		public function showShadow(): void
		{
			_alphaTo = 1;
			kickOffAnimation();
		}

		public function hideShadow(): void
		{
			_alphaTo = 0;
			kickOffAnimation();
		}

		private var _animating: Boolean = false;

		private function kickOffAnimation(): void
		{
			if (!_animating)
			{
				this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
				_animating = true;
			}
		}

		private function stopAnimation(): void
		{
			if (_animating)
			{
				this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
				_animating = false;
			}
		}

		protected function enterFrameHandler(event: Event): void
		{
			shadowSprite.alpha += (_alphaTo - shadowSprite.alpha) * ALPHA_SPEED;

			if (_glowEnabled)
			{
				glowFilter.alpha = shadowSprite.alpha;
				boundSprite.filters = [glowFilter];
			}

			if (Math.abs(shadowSprite.alpha - _alphaTo) < .05)
			{
				shadowSprite.alpha = _alphaTo;
				stopAnimation();
			}
		}
	}
}